<?php
session_start();

include 'inc/db_connect.php';
include 'inc/db_op.php';
include 'inc/html_op.php';
include 'inc/form_op.php';

function get_task_modify_table($task_id) {
  $sql = "select title, content, is_trip_out, project_id, assignto, progress, deadline, scheduled_start_time from task_info where id = $task_id";
  $rows = db_get($sql);
  $new_task_project_select_div_id = '"new_modify_task_project_select"';
  
  if(count($rows) > 0) {
    $row = $rows[0];
    $project_name = "";
    $init_project_option = "<option value=-1>无选项</option>";
    $is_trip_out=$row['is_trip_out'];
    if($row['project_id'] > 0) {
      $project_name = get_project_name_by_id($row['project_id']);
      $init_project_option = "<option value=".$row['project_id'].">$project_name</option>";
    }
    $task_modify_rows = array(
      "标题"=>"<input type=text name=title required size=40 value='".$row['title']."'/>",
      "内容"=>"<textarea rows=12 cols=50 name=content required>".$row['content']."</textarea>",
	    "计划开始时间"=>"<input type=date name=scheduled_start_time value=".$row['scheduled_start_time'].">",
		"完成期限"=>"<input type=date name=deadline value=".$row['deadline'].">",
		"分配给"=>get_assignto_select($_SESSION["user_id"], $row['assignto']).' <input type=checkbox name=mailassignment value=1/>发送邮件提醒',
		"重要等级"=>gen_select("priority",array("高"=>"高","中"=>"中","低"=>"低")),
		"出差"=>"<input type=checkbox name='is_trip_out' value=1 ".($is_trip_out==1?"checked='checked'":"")." />",
		"项目"=>"<input type=text name=project_name placeholder='请填写项目名称检索'".
  		" onchange='get_project_select($new_task_project_select_div_id, this.value)' />".
		"<br><select id=$new_task_project_select_div_id name=project_id>$init_project_option</select>",
      //"产品"=>get_product_options(),
	    "进度"=>"<input type=number name='progress' min=0 max=100 step=10 value=".$row['progress'].">%",
        ""=>"<button type=submit name=modify_task_submit id=modify_task_submit>提交</button>"
    );
    return  "<form method='post' name='task_modify' id='task_modify.php'>
      <input type=hidden name='id' value='".$task_id."'/>"
      .gen_twincol_table("task_modify", $task_modify_rows)
      ."</form>";

  } else {
    return "错误，未找到要修改的记录。";
  }
}

function get_wlog_modify_table($wlog_id) {
  $sql = "select end_time, title, content, task_id, updated_progress, work_hours, overtime_hours, 
    health from work_log where id = $wlog_id";
  $rows = db_get($sql);
  
  if(count($rows) > 0) {
    $row=$rows[0];
    $task_title = "";
    if($row['task_id'] > 0) {
      $task_rows = db_get("select title from task_info where id = ".$row['task_id']);
      if(count($task_rows) > 0) {
        $task_title = $task_rows[0]["title"];
      }
    
    }
    $wlog_modify_rows = array(
      "任务"=>"<input type=text disabled value='".$task_title."' />",
      "日期"=>"<input type=date name=end_time required value=".$row['end_time']." />",
      "标题"=>"<input type=text name=title required value='".$row['title']."'/>",
      "内容"=>"<textarea rows=12 cols=50 name=content required>".$row['content']."</textarea>",
      "进度"=>"<input type=number name=updated_progress required min=0 max=100 value='"
        .$row['updated_progress']."'/>%",
      "耗时"=>"<input type=number name=work_hours required min=0 max=24 step=0.25 value='"
        .$row['work_hours']."'/>",
      "加班小时"=>"<input type=number name=overtime_hours required min=0 max=24 step=0.25 value='"
        .$row['overtime_hours']."'/>",
     
     	"身体情况"=>gen_select("health",
    array("健康"=>"健康", "良好"=>"良好", "疲劳"=>"疲劳", "不适"=>"不适", "生病"=>"生病")),
      ""=>"<button name='wlog_modify_submit' type='submit'>提交</button>");

    return  "<form method='post' name='wlog_modify' id='wlog_log' action=wlog.php>
      <input type=hidden name='id' value='".$wlog_id."'/>"
      .gen_twincol_table("wlog_modify", $wlog_modify_rows)
      ."</form>";
  } else {
    return "错误，未找到可修改记录。";
  }
}

function get_work_outline_id_by_year_week_taskid($year, $week, $task_id) {
    $select_sql = "select id from work_outline where year = $year and week = $week and task_id = $task_id order by year, week desc";
    $query_result = db_get($select_sql);
    if (count($query_result) == 0) {
        $query_result = null;
    } else {
        $query_result = $query_result[0]['id'];
    }
    return $query_result;
}

function update_work_outline_table($name, $value) {
    $year = $_POST['year'];
    $week = $_POST['week'];
    $task_id = $_POST['task_id'];
    
    $id = get_work_outline_id_by_year_week_taskid($year, $week, $task_id);
    if (isset($id)) {
        $update_sql = "update work_outline set $name = '$value' where id = $id";
        mysql_query($update_sql);
    } else {
        $insert_sql = "insert into work_outline(year, week, task_id, $name) values($year, $week, $task_id, '$value')";
        if (!mysql_query($insert_sql)) {
            $value = '';
        }
    }
    return str_replace_cr_lf_to_br($value);
}

function update_work_outline() {
    return update_work_outline_table('outline', $_POST['text']);
}

function update_work_outline_comment() {
    return update_work_outline_table('outline_comment', $_POST['text']);
}

function update_work_plan() {
    return update_work_outline_table('plan', $_POST['text']);
}

function update_work_plan_comment() {
    return update_work_outline_table('plan_comment', $_POST['text']);
}

if(isset($_GET['query_project'])) {
  //$str="";
  if(isset($_GET['name_like'])) {
    $name_like = $_GET['name_like'];
    $sql = "select id, wbs_no, project_name from project_info where project_name like '%".$name_like."%'";
    $rows = db_get($sql);
    foreach($rows as $row) {
      $id = $row['id'];
      $name = $row['project_name'];
      $wbs_no = $row['wbs_no'];
      echo "<option value=$id>$wbs_no, $name</option>";
    }
  }
}

if(isset($_GET['modify_wlog_form_id'])) {
  echo get_wlog_modify_table($_GET['modify_wlog_form_id']);
}

if(isset($_GET['modify_task_form_id'])) {
  echo get_task_modify_table($_GET['modify_task_form_id']);
}

if (isset($_POST['update_work_outline_type'])) {
    $update_work_outline_type = $_POST['update_work_outline_type'];
    if ($update_work_outline_type == 'outline') {
        echo update_work_outline();
    } else if ($update_work_outline_type == 'outline_comment') {
        echo update_work_outline_comment();
    }
}
if (isset($_POST['update_work_plan_type'])) {
    $update_work_plan_type = $_POST['update_work_plan_type'];
    if ($update_work_plan_type == 'plan') {
        echo update_work_plan();
    } else if ($update_work_plan_type == 'plan_comment') {
        echo update_work_plan_comment();
    }
}

?>